"""
    requests:
    1.requests是第三方的http请求库。
    2.使用：

    需求：登录古诗文网，获取首页
    思路：发送两次请求：1.请求登录界面，返回两个参数  2.请求登录，指定两个参数，用户名，密码，验证码

"""
import requests
from lxml import etree
# 登录界面信息获取
url = "https://so.gushiwen.cn/user/login.aspx"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0",
}

session = requests.session()
response = session.get(url)
html = etree.HTML(response.text)
__VIEWSTATEGENERATOR = html.xpath("//input[@id='__VIEWSTATEGENERATOR']/@value")
__VIEWSTATE = html.xpath("//input[@id='__VIEWSTATE']/@value")
img_resp = session.get("https://so.gushiwen.cn/RandCode.ashx")
with open("1.png","wb") as file:
    file.write(img_resp.content)
# 登录
url = "https://so.gushiwen.cn/user/login.aspx"
code = input("输入验证码：")
params = {
    "__VIEWSTATE": __VIEWSTATE,
    "__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,
    "from": "http://so.gushiwen.cn/user/collect.aspx",
    "email": "17346551564",
    "pwd": "qq19930308",
    "code": code,
    "denglu": "登录"
}

response = session.get(url, params=params, headers=headers)
print(response.text)

